package krasa.mavenhelper.shxlutil;

import org.jetbrains.idea.maven.model.MavenArtifactNode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class TreeNode implements Serializable {

    private Long id;
    /**
     * jar包名称
     */
    private String jarName;
    private Long parentId;
    private Scope scope;
    private List<TreeNode> treeNodes;

    private DependencyMode dependencyMode;
    private String projectName;

    // 递归收集子节点
    private void collectChildren(TreeNode node, List<TreeNode> result) {
        if (node.treeNodes != null) {
            for (TreeNode child : node.treeNodes) {
                result.add(child);
                collectChildren(child, result);
            }
        }
    }
    // 获取所有子节点
    public List<TreeNode> getAllChildren() {
        List<TreeNode> allChildren = new ArrayList<>();
        collectChildren(this, allChildren);
        return allChildren;
    }


    public static TreeNode convertMavenNodeToTreeNode(MavenArtifactNode mavenNode) {
        TreeNode treeNode = new TreeNode();
        treeNode.setJarName(mavenNode.getArtifact().getLibraryName());
        if (mavenNode.getDependencies() != null && !mavenNode.getDependencies().isEmpty()) {
            List<TreeNode> childNodes = new ArrayList<>();
            for (MavenArtifactNode child : mavenNode.getDependencies()) {
                TreeNode childTreeNode = convertMavenNodeToTreeNode(child);
                childNodes.add(childTreeNode);
            }
            treeNode.setTreeNodes(childNodes);
        }

        return treeNode;
    }



    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getJarName() {
        return jarName;
    }

    public void setJarName(String jarName) {
        this.jarName = jarName;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Scope getScope() {
        return scope;
    }

    public void setScope(Scope scope) {
        this.scope = scope;
    }

    public List<TreeNode> getTreeNodes() {
        return treeNodes;
    }

    public void setTreeNodes(List<TreeNode> treeNodes) {
        this.treeNodes = treeNodes;
    }

    public DependencyMode getDependencyMode() {
        return dependencyMode;
    }

    public void setDependencyMode(DependencyMode dependencyMode) {
        this.dependencyMode = dependencyMode;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }
}